package com.example.mylistview;

import java.util.Arrays;
import java.util.LinkedList;

import com.putclub.ui.PullToRefreshListView;
import com.putclub.ui.PullToRefreshListView.OnRefreshListener;

import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
    private LinkedList<String> mListItems;
    View list_foot;
    PullToRefreshListView listView;
    ProgressBar listview_foot_progress;
    ArrayAdapter<String> adapter;
    TextView listview_foot_more;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		 listView=(PullToRefreshListView) findViewById(R.id.list);
	 
		mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
		adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mListItems);
		
		 list_foot=getLayoutInflater().inflate(R.layout.listview_footer, null);
		
		 listview_foot_more=(TextView)list_foot. findViewById(R.id.listview_foot_more);
		listview_foot_progress=(ProgressBar)list_foot. findViewById(R.id.listview_foot_progress);
		
		listView.addFooterView(list_foot);
		listView.setAdapter(adapter);
		
		listView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				
				if (view==null) {
					return; 
				}

				try {
					System.out.println("view.getPositionForView(list_foot)"+view.getPositionForView(list_foot)+"  "+view.getLastVisiblePosition());
					
					if (view.getPositionForView(list_foot) == view
							.getLastVisiblePosition())
						scrollEnd = true;
					listview_foot_progress.setVisibility(ProgressBar.VISIBLE);
					listview_foot_more.setText("正在加载中....");
				} catch (Exception e) {
					scrollEnd = false;
					System.out.println("有异常...");
				}
				
				if (scrollEnd) {
					System.out.println("加载下一页哟....");
					
					
					new Thread(){
						public void run() {
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							handler.sendEmptyMessage(2);
						};
					}.start();	
				}else {
//					System.out.println("。。");
				}

			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub
				listView.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		
		
		listView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
				
			}
		});
		
		listView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				// TODO Auto-generated method stub
//				  加载新闻zhong。。。。
				System.out.println("  加载新闻zhong..");
				
				new Thread(){
					public void run() {
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						handler.sendEmptyMessage(1);
					};
				}.start();
				
				
			}
		});
		
	
	}
	Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			System.out.println("msg.what"+msg.what);
			switch (msg.what) {
			/**
			 * 头部刷新..
			 */
			case 1:
//				listView.
				mListItems.addFirst("new "+System.currentTimeMillis());
				
				adapter.notifyDataSetChanged();
				
				listView.onRefreshComplete(">>>>>>");
				
				listView.setSelection(0);
				break;
			/**
			 * 下面的刷新...
			 */
			case 2:
				mListItems.add("new "+System.currentTimeMillis());
				adapter.notifyDataSetChanged();
				
				listview_foot_more.setText("更多..");
				listview_foot_progress.setVisibility(ProgressBar.GONE);
				
				listView.onRefreshComplete();
				break;

			default:
				break;
			}
		}
		
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	 private String[] mStrings = {
	            "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam",
	            "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
	            "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
	            "Allgauer Emmentaler"};

}
